User = function () 
{

};
User.Name = "user";
User.datas = new Array();
User.id = 0;

User.getAllType = function(){
    var url = Contants.Url + "groupuser";    
    $.ajax({
        url: url,
        type: "POST",
        data: {fn: -1},
        dataType: "Json",
        cache: false,
        success: function (data) {                        
            $("#type_id").find('option').remove();
            console.dir(data);
            var type_id = document.getElementById("type_id");
            for (var i = 0; i < data.length; i++) {               
                var optionNode = document.createElement("option");
                optionNode.setAttribute("value", data[i].id);
                optionNode.text = data[i].group_name;
                type_id.appendChild(optionNode);
            }
        }
    });
}

User.getAll = function () 
{
    var url = Contants.Url + User.Name;
    User.datas = new Array();
    $.ajax({
        url: url,
        type: "POST",
        data: {fn: -1},
        dataType: "Json",
        cache: false,
        success: function (data) 
        {            
            $("#body").empty();
            console.dir(data);
            for (var i = 0; i < data.length; i++) 
            {
                User.datas.push(data[i]);
                var tr = "<tr>";
                tr += "<td>" + data[i].user_name + "</td>";
                tr += "<td>" + data[i].email + "</td>";
                tr += "<td>" + data[i].phone + "</td>";
                tr += "<td class='center'>";
                tr += "<a class='btn btn-success' onclick = User.Edit(" + i + ")>";
                tr += "<i class='icon-edit icon-white'></i> Edit</a>";
                tr += "<a class=\"btn btn-danger\" onclick = User.Delete(" + data[i].id + ") >";
                tr += "<i class=\"icon-trash icon-white\"></i> Delete </a>";
                tr += "</td>";
                tr += "</tr>";
                $("#body").append(tr);
            }
        }
    });
};

User.Edit = function(index)
{    
    if(index >= 0 && index < User.datas.length)
    {        
        var obj = User.datas[index];
        var u = new Array();
        u.push(obj);
        Contants.Save(u,User.Name);        
    }
    var url = Contants.Url + "editUser.jsp";
    location.href = url;
    
};

User.getItem = function()
{
    var u = Contants.read(User.Name);
    if(u.length > 0)
    {
        var obj = u[0];
        User.id = obj.id;
        $("#user_name").val(obj.user_name);
        $("#password").val(obj.password);
        $("#email").val(obj.email);
        $("#phone").val(obj.phone);
        $("#type_id").val(obj.type_id);
        $("#create_date").val(obj.create_date);
        $("#id").val(obj.id);
        $("#isdelete").val(obj.isdelete);
    }
};

User.Insert = function()
{
    var url = Contants.Url + User.Name;
    var user_name = $("#user_name").val();
    var password = $("#password").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var type_id = $("#type_id").val();
    var create_date = $("#create_date").val();
    var id = $("#id").val();
    var isdelete = $("#isdelete").val();
    var data = {fn: 0 ,
        user_name : user_name,
        password : password,
        email : email,
        phone : phone,
        type_id : type_id,
        create_date : create_date,
        id : id,
        isdelete : isdelete
    };
    console.dir(data);
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) 
        {            
            var t = parseInt(data,10);
            if(t > 0)
            {                
                location.href = Contants.Url + "user.jsp";
            }
        }
    });
};

User.Update = function()
{
    var url = Contants.Url + User.Name;
    var user_name = $("#user_name").val();
    var password = $("#password").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var type_id = $("#type_id").val();
    var create_date = $("#create_date").val();
    var isdelete = $("#isdelete").val();
    var data = {fn: 1 ,
        user_name : user_name,
        password : password,
        email : email,
        phone : phone,
        type_id : type_id,
        create_date : create_date,
        id : User.id,
        isdelete : isdelete
    };
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) 
        {            
            var t = parseInt(data,10);          
            location.href = Contants.Url + "user.jsp";
        }
    });
};

User.Delete = function(id)
{
    var url = Contants.Url + User.Name;
    a = confirm(Contants.DeleteMessage);
    if(a == false)
    {
        return;
    }
    var data = {fn: 2,
        id : id
    };
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) 
        {            
           var t = parseInt(data,10);
           User.getAll();
        }
    });
};
